//
//  JSTUserInfoViewController.m
//  KYKJPackDemo
//
//  Created by 01 on 17/6/14.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "JSTUserInfoViewController.h"
#import "JSTUserInfoHeaderView.h"
#import "SPKitExample.h"

@interface JSTUserInfoViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tabView;

@property (nonatomic, strong) JSTUserInfoHeaderView *headerView;

@property (nonatomic, strong) JSTUserPublicModel *userInfoModel;

@property (nonatomic, strong) UIView *footerView;

@end

@implementation JSTUserInfoViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    _userInfoModel = [[JSTUserPublicModel alloc] init];
    // 0.初始化内部子控件
    [self setUpAllSubViews];
    // 1.拿到用户信息数据
    [self setUpUserInfoData];
}

#pragma mark - setUp
- (void)setUpAllSubViews
{
    [self.view addSubview:self.tabView];
}

- (void)setUpUserInfoData
{
    [JSTNewServer requestUserInfo:_userId success:^(NSDictionary *dataDic) {
        // 1.将数据赋值给模型数据(垃圾KVC字典转模型都不会)
        _userInfoModel.iconUrl = [KYHandleDataTool getStr:[dataDic valueForKey:@"icon"]];
        _userInfoModel.nickName = [KYHandleDataTool getStr:[dataDic valueForKey:@"name"]];
        _userInfoModel.sex = [KYHandleDataTool getStr:[dataDic valueForKey:@"sex"]];
        _userInfoModel.birthDay = [NSString stringWithFormat:@"%@", [KYHandleDataTool getStr:[dataDic valueForKey:@"birth_month"]]];
        _userInfoModel.signature = [KYHandleDataTool getStr:[dataDic valueForKey:@"sign"]];
        _userInfoModel.accountNumber = [KYHandleDataTool getStr:[dataDic valueForKey:@"username"]];
        // 2.根据模型计算内部子控件高度&赋值
        [self.headerView createContent:_userInfoModel blockHeight:^(CGFloat height) {
            self.headerView.frame = CGRectMake(0, 0, kScreenWidth, height);
        }];
        [self.tabView reloadData];
    } error:^(NSString *errStr) {
        [MBProgressHUD showError:errStr];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    }];
}

#pragma mark - 数据源&代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    if (!cell) {
        cell = [[UITableViewCell alloc] init];
    }
    return cell;
}
#pragma mark - method 私聊某人
- (void)changeImPage
{
    if (kAppDelegate.isTaobaoIMLogin) {
        if ([_userInfoModel.accountNumber isEqualToString:[KYUserAccountTool shareUserData].userPhone]) {
            [MBProgressHUD showError:@"请选择其他用户发送消息" toView:self.view];
        }else{
            YWPerson *person = [[YWPerson alloc] initWithPersonId:[_userInfoModel.accountNumber MD5] appKey:[KYUserAccountTool shareUserData].taobaoKey];
            [[SPKitExample sharedInstance] exampleOpenConversationViewControllerWithPerson:person fromNavigationController:self.navigationController];
        }
    } else {
        [MBProgressHUD showError:@"请先登录" toView:self.view];
    }
}

#pragma mark - setter & getter
- (void)setUserId:(NSString *)userId
{
    _userId = userId;
}

#pragma mark - 懒加载创建子控件
- (UITableView *)tabView
{
    if (!_tabView){
        _tabView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tabView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
        _tabView.delegate = self;
        _tabView.dataSource = self;
        _tabView.tableHeaderView = self.headerView;
        _tabView.tableFooterView = self.footerView;
    }
    return _tabView;
}

- (JSTUserInfoHeaderView *)headerView
{
    if (!_headerView)
    {
        _headerView = [[JSTUserInfoHeaderView alloc] initWithFrame:CGRectZero];
    }
    return _headerView;
}

- (UIView *)footerView
{
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 300)];
        UIButton *imBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        imBtn.frame = CGRectMake((kScreenWidth - kCurrentScreenWidth(75))/2, kCurrentScreenWidth(70), kCurrentScreenWidth(90), kCurrentScreenWidth(35));
        [imBtn setTitle:@"发送消息" forState:UIControlStateNormal];
        [imBtn setBackgroundColor:[UIColor orangeColor]];
        [imBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        imBtn.layer.cornerRadius = kCurrentScreenWidth(10);
        [imBtn addTarget:self action:@selector(changeImPage) forControlEvents:UIControlEventTouchUpInside];
        imBtn.backgroundColor = [UIColor blueColor];
        [_footerView addSubview:imBtn];
    }
    return _footerView;
}
@end
